exports.addH5sceneActivity = function (req, res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var new_oaid = 0;

    var completeCount = 0;

    var successCount = 0;

    //任务流创建
    async.waterfall([
        //1.权限验证
        /*function (cb) {
            P.permissionValidation(req, res, [CORE_APIConfig.coreAPI.addOrganizationActivity], function () {
                cb()
            });
        },*/

            //添加h5场景活动基本信息
            function (cb) {
                //console.log("---------");
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addOrganizationActivity,
                        method: 'POST',
                        data: {
                            oa_title        : bodyParams.activity_name,
                            oa_type         : "3",
                            oa_oid          : bodyParams.oid,
                            oa_category_text: bodyParams.category_text,
                            oa_start_time   : bodyParams.activity_start_time,
                            oa_end_time     : bodyParams.activity_end_time,
                            oa_explain      : bodyParams.activity_note,
                            oa_title_image  : bodyParams.activity_image
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "活动新增失败，请重新尝试");
                        }

                        new_oaid = data;

                        cb();
                    }
                );

            },

            //添加h5场景活动扩展信息
            function(cb){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addH5sceneActivityConfig,
                        method: 'POST',
                        data: {
                            hc_oaid      : new_oaid,
                            hc_style_url : bodyParams.style_url,
                            hc_bgmusicurl: bodyParams.bgmusicurl == undefined ? '' : bodyParams.bgmusicurl,
                            hc_ispay     : bodyParams.ispay == undefined ? '' : bodyParams.ispay,
                            hc_paymoney  : bodyParams.paymoney == undefined ? '' : bodyParams.paymoney,
                        }
                    },
                    function (err, data) {

                        //console.log(data);

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "活动新增失败，请重新尝试");
                        }

                        cb();
                    }
                );

            },

            //增加h5场景活动内容

            function(cb){

                var contentArr = JSON.parse(bodyParams.activity_content_data);
                //var contentArr = bodyParams.activity_content_data;

                //console.log(contentArr);

                successCount  = contentArr.length;

                completeCount = 0;
                //创建添加任务
                var tasks = [];

                //console.log(contentArr);

                //循环节点列表
                contentArr.forEach(function (item, i) {
                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.addH5sceneActivityContent,
                                method: 'POST',
                                data: {
                                    hc_type      : item.type,
                                    hc_order     : i,
                                    hc_bgimage   : item.bgimage,
                                    hc_title     : item.title,
                                    hc_image     : item.image,
                                    hc_oaid      : new_oaid

                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                //判断是否有错误信息
                                if(err) {
                                    //console.log("添加错误");
                                }else{
                                    completeCount++;
                                }

                                cb();
                            }
                        );
                    });
                });
                async.waterfall(tasks, cb);
            },

            // //判断写入结果
            function(cb){

                if(successCount == completeCount){
                    cb();
                }else{
                    return RES.response(res, false, "活动新增失败，请重新尝试");
                }
            }

        ], function (err, result) {
            //返回最终拼接结果
            RES.response(res, true, resultData);
        }
    );
}